// page/index/index.js
//获取应用实例

Page({
    data: {
        isHide: 0,
        imgUrls: [],
        //图标链接
        feaItems: [],
        //限时特价
        scrollItems: [],
        //鲜花用途       
        useItems: [],
        //鲜花
        listItems: [],
        //优惠券
        coupons: [],
        hasGet: [],
        ztitle: '享买花',
    },

    onLoad: function (options) {
       
        var scene = decodeURIComponent(options.scene)
        if (scene) {
            wx.setStorageSync('pid', scene) // 存储
        }

        var app = getApp()
        var that = this;
        wx.login({
            success: function (res) {
                wx.request({
                    url: app.globalData.host + '&g=App&m=Index&a=login',
                    data: { code: res.code },
                    header: {
                        'content-type': 'application/json'
                    },
                    success: function (res1) {
                        var session_key = res1.data.session_key
                        var openid = res1.data.openid
                        wx.getUserInfo({
                            success: function (res) {
                                var userInfo = res.userInfo
                                var adheadimg = userInfo.avatarUrl
                                var adnickName = userInfo.nickName
                                var encryptedData = res.encryptedData
                                var iv = res.iv
                                wx.request({
                                    url: app.globalData.host + '&g=App&m=Index&a=userinfo', //仅为示例，并非真实的接口地址
                                    data: {
                                        encryptedData: encryptedData, iv: iv, sessionKey: session_key, scene: scene
                                    }, header: {
                                        'content-type': 'application/json'
                                    },
                                    success: function (res) {
                                        console.log(res.data)
                                        var uid = parseInt(res.data)
                                        wx.setStorageSync('uid', uid) // 存储
                                        wx.setStorageSync('openid', openid) // 存储
                                        wx.setStorageSync('img', adheadimg) // 存储
                                        wx.setStorageSync('name', adnickName) // 存储
                                    }
                                })
                            }
                        })
                    }
                })
            }
        })
        // }

        wx.request({
            url: app.globalData.host + '&g=App&m=Index&a=asd', //仅为示例，并非真实的接口地址
            data: {},
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                //   console.log(res.data.titl)
                that.setData({
                    imgUrls: res.data.b,
                    feaItems: res.data.m,
                    scrollItems: res.data.t,
                    listItems: res.data.g,
                    useItems: res.data.use,
                    coupons: res.data.coup,
                    ztitle: res.data.titl
                })
            }
        })
    },
    onShow: function () {
        var that = this;
        var app = getApp()
        var uid = wx.getStorageSync('uid');
        console.log(uid)
        wx.request({
            url: app.globalData.host + '&g=App&m=Index&a=exconp', //仅为示例，并非真实的接口地址
            data: { uid: uid },
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                that.setData({
                    hasGet: res.data,
                    isHide: 1
                })
            }
        })

        wx.removeStorage({
            key: 'curIndex',
            success: function (res) {
            }
        })
        wx.removeStorage({
            key: 'toView',
            success: function (res) {
            }
        })
    },
    openPage: function (e) {
        wx.setStorage({
            key: 'curIndex',
            data: e.currentTarget.dataset.idx,
        })
        wx.setStorage({
            key: 'toView',
            data: e.currentTarget.dataset.id,
        })
    },
    sas: function (e) {
        var text = e.detail.value
        if (text) {
            wx.navigateTo({
                url: '/page/category/sousuo?text=' + text
            })
        }

    },
    ontel: function () {
        wx.makePhoneCall({ phoneNumber: '400-889-8188' })
    },
    getCoupons: function (e) {
        var that = this
        var uid = wx.getStorageSync('uid');
        var app = getApp()
        const index = e.currentTarget.dataset.id;
        var arr = that.data.coupons
        let hasGet = that.data.hasGet;
        const selected = hasGet[index];
        for (var i = 0; i <= arr.length - 1; i++) {

            if (i == index) {
                if (selected == false) {
                    wx.request({
                        url: app.globalData.host + '&g=App&m=Index&a=conp', //仅为示例，并非真实的接口地址   '领取成功赶快去使用吧'
                        data: { uid: uid, cid: arr[i]['id'] },
                        header: {
                            'content-type': 'application/json'
                        },
                        success: function (res) {
                            // console.log(res.data)
                            if (res.data.status == 1) {
                                wx.showModal({
                                    title: '提示',
                                    content: res.data.msg,
                                    showCancel: false
                                })
                            } else {
                                wx.showModal({
                                    title: '提示',
                                    content: res.data.msg,
                                    showCancel: false
                                })
                            }

                        }
                    })
                    hasGet[index] = !selected;
                    that.setData({
                        hasGet: hasGet
                    })

                } else {
                    wx.showModal({
                        title: '提示',
                        content: '优惠券已领取过了',
                        showCancel: false
                    })
                }

                break
            }
        }


    },
    onShareAppMessage: function (res) {
        var that = this
        if (res.from === 'button') {
            //   console.log(res.target)
        }
        //   console.log(that.data,)
        return {

            title: that.data.ztitle,
            path: '/page/index/index',
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    }, nav:function(e){
        
        if (e.currentTarget.dataset.id==5){
            wx.setStorageSync('tnt', 5) // 存储
        }
    },tz:function(){
        // console.log(123);
        wx.setStorageSync('tnt', 5) // 存储
        wx.switchTab({
            url: '/page/category/category'
        })
    }
})